package cate.common.util;


import cate.common.table.d.GD;

import java.util.ArrayList;
import java.util.List;

public class XT {

    public static boolean isBlank(List list) {
        if (list == null) {
            return true;
        }
        return list.size() <= 0;
    }

    public static boolean isNotBlank(List list) {
        return !isBlank(list);
    }

    /** 随机数双闭合区间 [min,max] */
    public static int randomRange(int min, int max) {
        int c_min = min;
        int c_max = max;
        min = Math.min(c_min, c_max);
        max = Math.max(c_min, c_max);
        return min + randomIndex(max + 1 - min);
    }

    /** 随机index */
    public static int randomIndex(int size) {
        return (int) Math.floor(Math.random() * (double) size);
    }

    /** 从数组中随机取几个元素 */
    public static <T> List<T> randomPick(List<T> src, int wantNum, boolean newList) {
        if (wantNum >= src.size()) {
            if (newList) {
                return new ArrayList<>(src);
            } else {
                return src;
            }
        }
        int removeNum = src.size() - wantNum;
        List<T> result = newList ? new ArrayList<>() : src;
        if (newList) {
            result.addAll(src);
        }
        while (removeNum > 0) {
            result.remove(randomIndex(result.size()));
            removeNum--;
        }
        return result;
    }

    public static <T> List<T> mergeNew(List<T> listA, List<T> listB) {
        List<T> result = new ArrayList<>();
        if (isNotBlank(listA)) {
            result.addAll(listA);
        }
        if (isNotBlank(listB)) {
            result.addAll(listB);
        }
        return result;
    }

    public static <T> List<T> merge(List<T> old, List<T> append) {
        if (XT.isBlank(append)) {
            return old;
        }
        if (old == null) {
            old = new ArrayList<>(append);
        } else {
            old.addAll(append);
        }
        return old;
    }

    /** 概率(默认万分比) */
    public static boolean chance(int a) {
        return chance(a, GD.W_10000);
    }

    /** 概率 */
    public static boolean chance(int a, int b) {
        double da = a;
        double db = b;
        return Math.random() < (da / db);
    }

}
